/*
 * TurnController.java
 *
 * Created on May 6, 2007, 9:12 PM
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.gamestate.controller;

import com.jme.input.controls.GameControl;
import com.jme.math.Quaternion;
import com.jme.scene.Controller;
import com.jme.scene.Node;

/**
 *
 * @author DGronau
 */
public class TurnController extends Controller {
    
    private GameControl left;
    private GameControl right;
    private GameControl up;
    private GameControl down;
    private Node node;
    private float speed;
    
    private static float hAngle = 0;
    private static float vAngle = 0;
    
    /** Creates a new instance of TurnController */
    public TurnController(GameControl left, GameControl right,
            GameControl up, GameControl down, Node node, float speed) {
        this.left = left;
        this.right = right;
        this.up = up;
        this.down = down;
        this.node = node;
        this.speed = speed;
    }

    public void update(float time) {
        float horizontal = left.getValue() - right.getValue();
        float vertical = down.getValue() - up.getValue();
        if (horizontal == 0.0f && vertical == 0.0f) {
            return;
        }
        hAngle += speed * time * horizontal;
        float newVAngle = vAngle + speed * time * vertical;
        if (-0.4 < newVAngle && newVAngle < 0.4) {
            vAngle = newVAngle;
        }
        node.setLocalRotation(new Quaternion(new float[]{vAngle, hAngle, 0f}));
    }
    
}
